package com.example.wechat;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.List;

public class MessageAdapter extends ListAdapter<Message, MessageAdapter.MessageViewHolder> {
    private static final int VIEW_TYPE_TEXT_SENT = 1;
    private static final int VIEW_TYPE_TEXT_RECEIVED = 2;
    private static final int VIEW_TYPE_IMAGE_SENT = 3;
    private static final int VIEW_TYPE_IMAGE_RECEIVED = 4;
    private static final int VIEW_TYPE_VOICE_SENT = 5;
    private static final int VIEW_TYPE_VOICE_RECEIVED = 6;

    private String currentUserId;
    private OnMessageClickListener listener;
    private final SimpleDateFormat timeFormat;

    public MessageAdapter(String currentUserId) {
        super(new MessageDiffCallback());
        this.currentUserId = currentUserId;
        this.timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
    }

    public interface OnMessageClickListener {
        void onImageClick(Message message);
        void onVoiceClick(Message message);
        void onRetryClick(Message message);
    }

    public void setOnMessageClickListener(OnMessageClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        Message message = getItem(position);
        boolean isSent = message.getSenderId().equals(currentUserId);
        
        switch (message.getType()) {
            case Message.TYPE_TEXT:
                return isSent ? VIEW_TYPE_TEXT_SENT : VIEW_TYPE_TEXT_RECEIVED;
            case Message.TYPE_IMAGE:
                return isSent ? VIEW_TYPE_IMAGE_SENT : VIEW_TYPE_IMAGE_RECEIVED;
            case Message.TYPE_VOICE:
                return isSent ? VIEW_TYPE_VOICE_SENT : VIEW_TYPE_VOICE_RECEIVED;
            default:
                return VIEW_TYPE_TEXT_SENT;
        }
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        switch (viewType) {
            case VIEW_TYPE_TEXT_SENT:
                view = inflater.inflate(R.layout.item_message_sent, parent, false);
                return new TextMessageViewHolder(view);
            case VIEW_TYPE_TEXT_RECEIVED:
                view = inflater.inflate(R.layout.item_message_received, parent, false);
                return new TextMessageViewHolder(view);
            case VIEW_TYPE_IMAGE_SENT:
                view = inflater.inflate(R.layout.item_message_sent, parent, false);
                return new ImageMessageViewHolder(view);
            case VIEW_TYPE_IMAGE_RECEIVED:
                view = inflater.inflate(R.layout.item_message_received, parent, false);
                return new ImageMessageViewHolder(view);
            case VIEW_TYPE_VOICE_SENT:
                view = inflater.inflate(R.layout.item_message_sent, parent, false);
                return new VoiceMessageViewHolder(view);
            case VIEW_TYPE_VOICE_RECEIVED:
                view = inflater.inflate(R.layout.item_message_received, parent, false);
                return new VoiceMessageViewHolder(view);
            default:
                view = inflater.inflate(R.layout.item_message_sent, parent, false);
                return new TextMessageViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        Message message = getItem(position);
        holder.bind(message);
    }

    abstract class MessageViewHolder extends RecyclerView.ViewHolder {
        TextView timeText;

        MessageViewHolder(View itemView) {
            super(itemView);
            timeText = itemView.findViewById(R.id.text_message_time);
        }

        abstract void bind(Message message);
    }

    class TextMessageViewHolder extends MessageViewHolder {
        private final TextView messageText;

        TextMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            messageText = itemView.findViewById(R.id.text_message_body);
        }

        @Override
        void bind(Message message) {
            messageText.setText(message.getContent());
            timeText.setText(timeFormat.format(new Date(message.getTimestamp())));
        }
    }

    class ImageMessageViewHolder extends MessageViewHolder {
        private final ImageView messageImage;

        ImageMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            messageImage = itemView.findViewById(R.id.message_image);
        }

        @Override
        void bind(Message message) {
            // TODO: 加载图片
            timeText.setText(timeFormat.format(new Date(message.getTimestamp())));
            
            messageImage.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onImageClick(message);
                }
            });
        }
    }

    class VoiceMessageViewHolder extends MessageViewHolder {
        private final TextView durationText;

        VoiceMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            durationText = itemView.findViewById(R.id.duration_text);
        }

        @Override
        void bind(Message message) {
            durationText.setText(message.getVoiceDuration() + "\"");
            timeText.setText(timeFormat.format(new Date(message.getTimestamp())));
            
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onVoiceClick(message);
                }
            });
        }
    }

    private static class MessageDiffCallback extends DiffUtil.ItemCallback<Message> {
        @Override
        public boolean areItemsTheSame(@NonNull Message oldItem, @NonNull Message newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull Message oldItem, @NonNull Message newItem) {
            return oldItem.getStatus() == newItem.getStatus() &&
                    oldItem.isRead() == newItem.isRead();
        }
    }
} 